<?php


namespace App\Http\Controllers\App\V3;


use App\Enums\RedisKeyEnum;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use App\Services\Tools\PushService;
use Illuminate\Http\Request;

class InteractionFaceController extends BaseController
{
    public function lists(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'roomId' => 'required|numeric|gt:0',
            ]
        );
        if ($v->fails()) throw new BasicException(10010);

        $room_id = $request->input('roomId', 1);
        $result = app('repo_interaction_face')->lists($room_id);

        return Response::sendData($result, "获取成功");
    }

    public function use_interaction_face(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'roomId'      => 'required|numeric|gt:0',
                'face_id'     => 'required|numeric|gt:0',
                'pit_use'     => 'required|numeric|gt:0',
                'pit_be_used' => 'required|numeric|gt:0',
            ]
        );
        if ($v->fails()) throw new BasicException(10010);
        $room_id = $request->input('roomId', 1);
        $face_id = $request->input('face_id', 1);
        $is_use = app('redis')->get(sprintf(RedisKeyEnum::ROOM_FACE,$room_id));
        if (!empty($is_use)) throw new BasicException(40200);
        $result = app('repo_interaction_face')->use_interaction_face($room_id, $face_id);
        // 推送消息
        $special_time = [
            'start' => 0,
            'end'   => 0
        ];
        if (!empty($result['show_time'])){
            $special_time = json_decode($result['show_time'],true);
        }
        $pit_params = $request->only('pit_use', 'pit_be_used');
        $push = new PushService(UID, $room_id);
        $push->setDelay(0);
        $push->interaction_face($pit_params['pit_use'],$pit_params['pit_be_used'],$result['picture'],$result['special_start'],$special_time['start'],$result['special_running'],$result['special_end'],$special_time['end']);
        return Response::sendData($result, "使用成功");
    }
}